package com.sttg.csp.registration.action;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.sttg.csp.common.action.PagedAction;
import com.sttg.csp.common.consts.SystemConst;
import com.sttg.csp.common.exception.BizException;
import com.sttg.csp.registration.service.CerAssocManageService;

public class CerAssocManageAction extends PagedAction {

	private CerAssocManageService cerAssocManageService = CerAssocManageService.getInstance();

	private Map model = new HashMap();

	private File file;// 对应的java文件对象

	private String fileFileName;// 对应的java文件名称

	private String fileContentType;// 对应的java文件类型

	public CerAssocManageAction() {
	}

	// show Index page
	public String showIndex() throws Exception {
		return INDEX;
	}

	// query list of B/D administrators.
	public String queryList() throws Exception {
		try {
			String sessionUid = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_UID");
			String sessionDeparment = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_DEPARTMENT");
			model.put("SESSION_UID", sessionUid);
			model.put("SESSION_DEPARTMENT", sessionDeparment);
			rows = cerAssocManageService.queryList(model);
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return LIST;
	}

	// show detail of a B/D certificate association record 
	public String showInfo() throws Exception {
		try {
			model = cerAssocManageService.queryObject(model);
			if (model == null) {
				this.addActionError("The record no found in ldap!");
				return ERROR_PAGE;
			}
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR_PAGE;
		}
		setOperationType(SystemConst.OPERATION_TYPE_VIEW);
		return INFO;
	}

	// show page for activate or deactivate association
	public String showActivateOrDeactivate() throws Exception {
		try {
			model = cerAssocManageService.queryObject(model);
			if (model == null) {
				this.addActionError("The record no found in ldap!");
				return ERROR_PAGE;
			}
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR_PAGE;
		}
		return "activate_or_deactivate";
	}
	
	// perform to activate cert reg association record.
	public String doActivate() throws Exception {
		try {
			String sessionUid = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_UID");
			model.put("SESSION_UID", sessionUid);
			cerAssocManageService.doActivate(model);
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}
	
	// perform to deactivate cert reg association record.
	public String doDeactivate() throws Exception {
		try {
			String sessionUid = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_UID");
			model.put("SESSION_UID", sessionUid);
			cerAssocManageService.doDeactivate(model);
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}
	
	// perform to deactivate cert reg association record.
	public String doDelete() throws Exception {
		try {
			String sessionUid = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_UID");
			model.put("SESSION_UID", sessionUid);
			cerAssocManageService.doDelete(model);
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}
	
	// show page for pre-reg cert
	public String showPreReg() throws Exception {
		return "pre_reg";
	}
	
	// perform to pre-reg cert
	public String doPreReg() throws Exception {
		try {
			String sessionUid = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_UID");
			model.put("SESSION_UID", sessionUid);
			cerAssocManageService.doPreReg(model);
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return NONE;
	}

	// perform to export csv operation 
	public String doExportCSV() throws Exception {
		
		
        try {  
        	String sessionUid = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_UID");
			String sessionDeparment = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_DEPARTMENT");
			model.put("SESSION_UID", sessionUid);
			model.put("SESSION_DEPARTMENT", sessionDeparment);
			rows = cerAssocManageService.queryList(model);

			StringBuffer buff = new StringBuffer();
			buff.append("B/D uid")
					.append(",B/D user name")
					.append(",Department")
					.append(",Status")
					.append(",Certificate type")
					.append(",Validity start date")
					.append(",Validity_end_date")
					.append("\n");
			for (Iterator item = rows.iterator(); item.hasNext();) {
				Map each = (Map) item.next();
				buff.append((String) each.get("BD_UID"));
				buff.append("," + (String) each.get("BD_USER_NAME"));
				buff.append("," + (String) each.get("DEPARTMENT"));
				buff.append("," + (String) each.get("STATUS"));
				buff.append("," + (String) each.get("CER_TYPE"));
				buff.append("," + (String) each.get("VALIDITY_START_DATE"));
				buff.append("," + (String) each.get("VALIDITY_END_DATE"));
				buff.append("\n");
			}
			
			response.setHeader("Content-Disposition", "attachment;filename=certificate association records.csv");  
	        response.setContentType("APPLICATION/OCTET-STREAM");  
            response.getOutputStream().print(buff.toString());  
            response.getOutputStream().flush();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            try {  
                response.getOutputStream().close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
		return NONE;
	}
	
	public void setModel(Map model) {
		this.model = model;
	}

	public Map getModel() {
		return model;
	}

	
	public File getFile() {
		return file;
	}

	
	public void setFile(File file) {
		this.file = file;
	}

	
	public String getFileFileName() {
		return fileFileName;
	}

	
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	
	public String getFileContentType() {
		return fileContentType;
	}

	
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
}
